
---@class ent
---@field bh_mobius_sword_container_stuff_onplayer table<PrefabID,ent>


---@class bh_mobius_sword_container_stuff
---@field onequip nil|fun(inst:ent,owner:ent)
---@field onunequip nil|fun(inst:ent,owner:ent)

---@type table<PrefabID,bh_mobius_sword_container_stuff>
local map = {
    redgem = {},
    bluegem = {},
    purplegem = {},
    orangegem = {
        onequip = function (inst, owner)
            if owner and owner.components.locomotor then
                owner.components.locomotor:SetExternalSpeedMultiplier(inst,'bh_mobius_sword_container_stuff'..'orangegem',3)
            end
        end,
        onunequip = function (inst, owner)
            if owner and owner.components.locomotor then
                owner.components.locomotor:RemoveExternalSpeedMultiplier(inst,'bh_mobius_sword_container_stuff'..'orangegem')
            end
        end
    },
    greengem = {},
    yellowgem = {},
}

for k,_ in pairs(map) do
    AddPrefabPostInit(k,function (inst)
        inst:AddTag('bh_mobius_sword_container_stuff_cangocontianer')
    end)
end

---comment
---@param isequip boolean
---@param container ent
---@param gem ent
---@param gem_prefab PrefabID
---@param player ent
local function switchGem(isequip,container,gem,gem_prefab,player)
    if isequip then
        local onequip = map[gem_prefab].onequip
        if onequip then
            onequip(gem,player)
        end
    else
        local onunequip = map[gem_prefab].onunequip
        if onunequip then
            onunequip(gem,player)
        end
    end
end

AddPrefabPostInit('bh_mobius_sword',function (inst)
    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent('container')
    inst.components.container:WidgetSetup('bh_mobius_sword')

    inst:ListenForEvent('itemget',
    ---comment
    ---@param _ ent
    ---@param data event_data_itemget
    function (_,data)
        if data then
            local item = data.item
            if item then
                local item_prefab = item.prefab
                if item_prefab and map[item_prefab] then
                    -- 宝石放入箱子后,给箱子加一个tag,用于判断箱子里是否有对应的宝石了
                    inst:AddTag('bh_mobius_sword_container_has_'..item_prefab)
                    -- 获得宝石,添加对应的能力
                    local owner = inst.components.inventoryitem and inst.components.inventoryitem.owner or nil
                    if owner and owner:HasTag('player') and inst.components.equippable and inst.components.equippable:IsEquipped() then
                        switchGem(true,inst,item,item_prefab,owner)
                    end
                    -- 将宝石挂到人物上
                    if owner then
                        if owner.bh_mobius_sword_container_stuff_onplayer == nil then
                            owner.bh_mobius_sword_container_stuff_onplayer = {}
                        end
                        owner.bh_mobius_sword_container_stuff_onplayer[item_prefab] = item
                    end
                end
            end
        end
    end)

    inst:ListenForEvent('itemlose',
    ---comment
    ---@param _ ent
    ---@param data event_data_itemlose
    function (_,data)
        if data then
            local item = data.prev_item
            if item then
                local item_prefab = item.prefab
                if item_prefab and map[item_prefab] then
                    inst:RemoveTag('bh_mobius_sword_container_has_'..item_prefab)
                    local owner = inst.components.inventoryitem and inst.components.inventoryitem.owner or nil
                    if owner and owner:HasTag('player') and inst.components.equippable and inst.components.equippable:IsEquipped() then
                        switchGem(false,inst,item,item_prefab,owner)
                    end
                    -- 将挂在人物上的宝石移除
                    if owner and owner.bh_mobius_sword_container_stuff_onplayer then
                        owner.bh_mobius_sword_container_stuff_onplayer[item_prefab] = nil
                    end
                end
            end
        end
    end)

    local old_onequipfn = inst.components.equippable.onequipfn
    inst.components.equippable.onequipfn = function (inst,owner,from_ground,...)
        local allitems = inst.components.container:GetAllItems()
        for _,item in ipairs(allitems or {}) do
            local item_prefab = item and item.prefab
            if item_prefab then
                if owner.bh_mobius_sword_container_stuff_onplayer == nil then
                    owner.bh_mobius_sword_container_stuff_onplayer = {}
                end
                owner.bh_mobius_sword_container_stuff_onplayer[item_prefab] = item
                switchGem(true,inst,item,item_prefab,owner)
            end
        end
        return old_onequipfn ~= nil and old_onequipfn(inst,owner,from_ground,...) or nil
    end

    local old_onunequipfn = inst.components.equippable.onunequipfn
    inst.components.equippable.onunequipfn = function (inst,owner,...)
        owner.bh_mobius_sword_container_stuff_onplayer = nil
        local allitems = inst.components.container:GetAllItems()
        for _,item in ipairs(allitems or {}) do
            local item_prefab = item and item.prefab
            if item_prefab then
                switchGem(false,inst,item,item_prefab,owner)
            end
        end
        return old_onunequipfn ~= nil and old_onunequipfn(inst,owner,...) or nil
    end
end)